﻿using cuiail.DBConnections;
using cuiail.DBModels;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Routing;
using System.Data;

namespace cuiail.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class TransportRobotController : ControllerBase
    {
        private readonly IWorkingModeService _workingModeService;
        private readonly ICurrentWorkModeService _currentWorkModeService;
        private readonly IPrintContentService _printContentService;
        private readonly ILogger<LabController> _logger;

        public static _搬运机器人_POS_状态 transportRobotStatus { get; set; } = new _搬运机器人_POS_状态();
        public static _搬运机器人_GET_命令 transportRobotCommand { get; set; } = new _搬运机器人_GET_命令();



        public TransportRobotController(IWorkingModeService workingModeService,ICurrentWorkModeService currentWorkModeService, IPrintContentService printContentService, ILogger<LabController> logger)
        {
            _currentWorkModeService = currentWorkModeService;
            _workingModeService = workingModeService;
            _printContentService= printContentService;
            _logger = logger;
        }


        /// <summary>
        /// 获取命令数据
        /// </summary>
        [HttpGet]
        [Route("GetCommandData")]
        public async Task<ActionResult<IEnumerable<_搬运机器人_GET_命令>>> GetCommandDataAsync()
        {
            try
            {
                if (CloudPlatformController.cloudPlatformPosWorkOrder != null)
                {
                   
                  //  transportRobotCommand._当前生成产品信息 = CloudPlatformController.cloudPlatformPosWorkOrder.ProductIdTable[0];
                    transportRobotCommand._系统模式数据 = CloudPlatformController.cloudSystemModel.WorkingModeId;
                    transportRobotCommand._系统命令 = CloudPlatformController.cloudSystemCommand.SystemCommand;
                    transportRobotCommand._质检岛允许放料信号  = QualityIslandController.qualityIslandStatus._搬运机器人允许放料信号_工序允许开始信号;
                    //if (QualityIslandController.qualityIslandStatus._当前工序状态.CompletionStatus ==ProcessCompletionStatus.Completed)
                    //{
                    //    transportRobotCommand._质检岛允许放料信号  = true;
                    //}
                    //else
                    //{
                    //    transportRobotCommand._质检岛允许放料信号  = false;
                    //}
                    transportRobotCommand._装配岛允许取料信号=AssemblyIslandController.assemblyIslandStatus._当前工序状态;
                    transportRobotCommand._工序ID=_工单管理类.AssignProcessIdByDeviceId(DeviceID.MobileRobot);

                }
                var result = transportRobotCommand;
                return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "get命令时发生错误");
                return StatusCode(500, "get命令时发生错误，请稍后重试");
            }
        }


        



        /// <summary>
        /// 写入巡检机器人状态数据
        /// </summary>
        /// <param name="currentWorkingMode">当前工作模式代码</param>
        /// <returns>写入成功或者失败</returns>
        [HttpPost]
        [Route("InsertTransportRobotStatusData")]
        public async Task<ActionResult<bool>> InsertTransportRobotStatusDataAsync(_搬运机器人_POS_状态 temp)
        {
            try
            {
                transportRobotStatus = temp;
                CloudPlatformController.cloudTransportRobotStatus.Updatefrom搬运机器人_POS_状态(transportRobotStatus);

                _工单管理类._编号工序流转( CloudPlatformController.cloudWorkOrder, transportRobotCommand._当前生成产品信息, temp._当前工序状态);

                 var result = true;
                return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "写入状态数据时发生错误");
                return StatusCode(500, "写入状态数据时发生错误，请稍后重试");
            }
        }

        /// <summary>
        /// 清除当前生产信息
        /// </summary>
        /// <param name="">当前工作模式代码</param>
        /// <returns>写入成功或者失败</returns>
        [HttpPost]
        [Route("ClearCurrentProductionInfos")]
        public async Task<ActionResult<bool>> ClearCurrentProductionInfosAsync(int processID)
        {
            try
            {

                _工单管理类.ClearModeProcessListByWorkOrderId(processID);
                transportRobotCommand._当前生成产品信息.ProductId="";
                return Ok(true);

            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "写入状态数据时发生错误");
                return StatusCode(500, "写入状态数据时发生错误，请稍后重试");
            }
        }









    }
}
